//
//  GLBadgeViewController.h
//  GLFormKitDemo
//
//  Created by 121 on 2018/3/28.
//  Copyright © 2018年 杭州吉利控股信息中心. All rights reserved.
//

#import "GLFormRecordCell.h"
#import <Masonry/Masonry.h>
@interface GLFormRecordCell()
@property(nonatomic,strong)UILabel *recordLabel;
@property(nonatomic,strong)UIImageView *circleImageView;
@end

@implementation GLFormRecordCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self =[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.separatorInset = UIEdgeInsetsMake(0, 0, -1, 0);
    UIImageView *leftLineImageView = [[UIImageView alloc] init];
    leftLineImageView.backgroundColor = [UIColor colorWithRed:228.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:1];
    [self.contentView addSubview:leftLineImageView];
    
    _circleImageView = [[UIImageView alloc] init];
    _circleImageView.image = [UIImage imageNamed:@"icon-其它跟进记录"];
    [self.contentView addSubview:_circleImageView];
    
    _timeLab = [[UILabel alloc] init];
    _timeLab.text =@"2016-08-07";
    _timeLab.font = [UIFont boldSystemFontOfSize:9];
    _timeLab.textColor = [UIColor lightGrayColor];
    _timeLab.font = [UIFont boldSystemFontOfSize:12.0f];
    _timeLab.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_timeLab];
    
    _recordLabel = [[UILabel alloc] init];
    _recordLabel.numberOfLines = 0;
    _recordLabel.font = [UIFont systemFontOfSize:14.0f];
    _recordLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_recordLabel];
    
    [_circleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(15);
        make.top.mas_equalTo(10);
        make.left.equalTo(self.contentView).offset(self.edgeMargin);
    }];
    
    [leftLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1.0/[UIScreen mainScreen].scale);
        make.top.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
        make.centerX.equalTo(_circleImageView.mas_centerX);
    }];
    
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_circleImageView);
        make.left.equalTo(_circleImageView.mas_right).offset(5/2);
        make.height.mas_equalTo(15);
    }];
    [_recordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_timeLab);
        make.right.equalTo(self.contentView).offset(-2*5);
        make.top.equalTo(_timeLab.mas_bottom).offset(5);
    }];

    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self hideSeparator];
    
}


-(void)setText:(NSString *)text{

    _recordLabel.attributedText = text?[self getAttributedStringWithString:text lineSpace:10.0f baselineOffset:0]:[[NSAttributedString alloc] initWithString:@""];
}

-(void)setIsCurRecord:(BOOL)isCurRecord{
    _isCurRecord = isCurRecord;
    if (_isCurRecord) {
        _circleImageView.image =[UIImage imageNamed:@"icon-当前跟进记录"];
    }else{
        _circleImageView.image = [UIImage imageNamed:@"icon-其它跟进记录"];
    }
}

/**
 *  调整行间距
 *
 *  @param string         传入字符串
 *  @param lineSpace      行间距
 *  @param baselineOffset 头部偏移距离
 *
 *  @return 富文本字符串
 */

-(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace baselineOffset:(CGFloat)baselineOffset {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 调整行间距
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    // 设置文本偏移量
    [attributedString addAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset) range:range];
    return attributedString;
}

+ (CGFloat)cellHeightByText:(NSString*)text
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10.0f; // 调整行间距
    NSMutableDictionary *attrsDict = [NSMutableDictionary dictionary];
    attrsDict[NSFontAttributeName] = [UIFont systemFontOfSize:14.0];
    attrsDict[NSParagraphStyleAttributeName] = paragraphStyle;
    attrsDict[NSBaselineOffsetAttributeName] = 0;
    CGFloat currWidth = [UIScreen mainScreen].bounds.size.width - 42;
    if(YES){
        currWidth-=20;
    }
    CGSize recordSize = [text boundingRectWithSize:CGSizeMake(currWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrsDict context:nil].size;
    //  LGLog(@"%f--%f===%f",width,recordSize.height,recordSize.width);
    
    return recordSize.height + 10 + 15 + 10;
}

@end
